/*
 * author      : dormi330
 * date        : 2018/6/26
 * description : adaptor 会员服务
 */

package com.deep.trade.adaptor.required.seller;

import com.deep.trade.adaptor.required.IdService;
import com.deep.trade.application.AppRuntimeEvn;
import com.deep.trade.common.TradeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class SellerServiceMock implements SellerService {
    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());
    private static Map<String, Seller> sellerDb = new HashMap<>();

    static {
        Seller seller1 = new Seller(IdService.SELLER_1_ID, "seller1", IdService.TENANT_CODE);
        sellerDb.put(IdService.SELLER_1_TOKEN, seller1);

        Seller seller2 = new Seller(IdService.SELLER_2_ID, "seller2", IdService.TENANT_CODE);
        sellerDb.put(IdService.SELLER_2_TOKEN, seller2);
    }

    @Autowired
    private AppRuntimeEvn appRuntimeEvn;

    @Override
    public Seller sellerOfToken() throws Exception {
        String tenantCode = appRuntimeEvn.getTenantCode();
        String token = appRuntimeEvn.getToken();
        return sellerDb.get(token);
    }

    @Override
    public Seller assureSellerOfToken() throws Exception {
        Seller seller = sellerOfToken();
        if (null == seller) throw new TradeException("cann't get seller info");
        return seller;
    }
}
